﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 27/12/2008
// Time: 22:35
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of frmRegisterCity.
	/// </summary>
	public partial class frmRegisterCity : csharp.common.RegisterTemplate
	{
		private City city = new City();
		private Cities listCity  = new Cities();
		private States listState = new States();
		private Countries listCountry = new Countries();
		
		//controlar a mudanca de valores dos combos boxes de pais e estados
		bool mutex= false;
		
		public frmRegisterCity()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		#region overrided methods
		
			protected override void newEvent()
			{
				city.clear();
				txtName.Focus();
			}
			
			/// <summary>
			/// Set data from edits to class
			/// </summary>
			protected override void setClass()
			{
				city.name=		txtName.Text;
				city.area=    	txtArea.Text;
				city.altitude=	txtAltitude.Text;
				city.latitude=	txtLatitude.Text;
				city.longitude=txtLongitude.Text;
				city.enable= 	cbxEnable.Checked;
				city.isCapital=cbxIsCapital.Checked;
				city.stateId=	(cmbState.SelectedItem as State).id;
			}
			
			/// <summary>
			/// Set data from class to edits
			/// </summary>
			protected override void setEdits()
			{
				city.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
				
				if (city.get())
				{
					txtName.Text= city.name;
					txtArea.Text= city.area;
					txtAltitude.Text = city.altitude;
					txtLatitude.Text = city.latitude;
					txtLongitude.Text = city.longitude;
					cbxEnable.Checked = city.enable;
					cbxIsCapital.Checked = city.isCapital;
					Functions.setSelectIndexComboStates(cmbState, city.stateId);
					
					listState.search(city.stateId);
					Functions.setSelectIndexComboCountries(cmbCountry, listState.get().countryId);
				}
			}
			
			/// <summary>
			/// Fill the DataGridView
			/// </summary>
			protected override void fillDataGridView()
			{
				int tam;
				listCity.fillList();
				
				tam = listCity.count();
				dgvMain.Rows.Clear();
				for (int i=0; i < tam; i++)
				{
					dgvMain.Rows.Add(new String[] {listCity.get(i).id, listCity.get(i).name});
				}				
			}
			
			/// <summary>
			/// Find the object of search
			/// </summary>
			protected override void find()
			{
				common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
			}		
			
		   /// <summary>
		   /// Save the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was saved sucessful, false case not.</returns>	
		   protected override bool save()
		   {
		   	return city.save();
		   }
		   
		   /// <summary>
		   /// Update the register in the database
		   /// </summary>
		   /// <returns>bool - true if the register was updated sucessful, false case not.</returns>
		   protected override bool update()
		   {   
		   	return city.update();
		   }
		   
		   /// <summary>
		   /// Delete the register in the database.
		   /// </summary>
		   /// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
		   protected override bool delete()
		   {
		   	city.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();	
		   	return city.delete();
		   }
		   
		   /// <summary>
		   /// Method call in initialization
		   /// </summary>
		   protected override void init()
		   {
		   	DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
		   	DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
		   	
		   	id.Name = "id";
		   	id.Visible = false;
		   	
		   	name.Name = "name";
		   	name.HeaderText = "Nome";
		   	
		   	dgvMain.Columns.AddRange(new DataGridViewColumn[] {id, name});
		   	
		   	listCountry.fillList();
		   	Functions.fillComboCountries(listCountry, cmbCountry,"id", "name");	
		   	
		   	listState.fillList();
		   	Functions.fillComboStates(listState, cmbState,"id", "name");
		   }
		#endregion
		
		void CmbCountrySelectedValueChanged(object sender, EventArgs e)
		{
			if ((cmbCountry.SelectedIndex >= 0) && (!mutex))
			{
				cmbState.SelectedIndex = -1;
				listState.fillList((cmbCountry.SelectedItem as Country).id);
				Functions.fillComboStates(listState, cmbState, "id", "name");
			}	
			mutex= false;
		}
		
		void CmbStateSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbState.SelectedIndex >= 0)
			{
				mutex= true;
				listState.search((cmbState.SelectedItem as State).id);
				Functions.setSelectIndexComboCountries(cmbCountry, listState.get().countryId);
				mutex= false;
			}
		}
	}
}
